<?php
// @formatter:off
/**
 * @file c.comic.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-08-22 23:54:04 +0000 (Thu, 22 Aug 2013) $
 *
 * $Id: c.comic.php 103 2013-08-22 23:54:04Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/themes/mdefault/controlers/contents/c.comic.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_COMIC_VIEW)) {
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";
	global $wctDefaults;

	if($wcComic && $wcComic->ok()) {
		$wcComic->expand();

		$wcThemeAssigns["PATH"] = "{$wcComic->directory()->name}/{$wcComic->name}";

		if(isset($_REQUEST["astable"])) {
			$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.comic-table.html";

			$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
			$minImgInPage = $wctDefaults["max-per-page"]["comic-astable"] * ($page - 1);
			$maxImgInPage = $minImgInPage + $wctDefaults["max-per-page"]["comic-astable"];

			$wcThemeAssigns["PATH"] .= " (page {$page})";

			$paramsNoImg = requestParams(true, $wcAction, array(
				"image",
				"astable"
			));

			$images = array();
			$imgCount = 0;
			foreach($wcComic->images() as $img) {
				if($imgCount >= $minImgInPage && $imgCount < $maxImgInPage) {
					$guess = $wcComic->previewUriFor($imgCount, false);
					$aux = array(
						"title" => $img->name,
						"link" => "{$paramsNoImg}&image={$imgCount}",
						"img" => $guess !== false ? $guess : $wcDefaults['preview']
					);
					$images[] = $aux;
				}
				$imgCount++;
			}

			$wcThemeAssigns["images"] = $images;

			wcSetPager(requestParams(true, $wcAction, array("image")), ceil($imgCount / $wctDefaults["max-per-page"]["comic-astable"]), $page);
		} else {
			global $wcThemeMobileDefault;

			$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.comic.html";

			$paramsNoImg = requestParams(true, $wcAction, array("image"));
			$index = isset($_REQUEST["image"]) ? $_REQUEST["image"] : 0;
			$files = $wcComic->images();
			$imgCount = count($files);

			$wcThemeAssigns["PATH"] .= " (".($index + 1)."/{$imgCount})";

			$first = 0;
			$previous = $index - 1;
			$current = $index;
			$next = $index + 1;
			$last = $imgCount - 1;

			if($previous < $first) {
				$previous = $last;
			}
			if($next > $last) {
				$next = $first;
			}

			$wcThemeAssigns["LNKFIRST"] = "{$paramsNoImg}&image={$first}";
			$wcThemeAssigns["LNKPREV"] = "{$paramsNoImg}&image={$previous}";

			$wcThemeAssigns["LNKTABLE"] = "{$paramsNoImg}&astable&page=".ceil(($index + 1) / $wctDefaults["max-per-page"]["comic-astable"]);

			$wcThemeAssigns["LNKNEXT"] = "{$paramsNoImg}&image={$next}";
			$wcThemeAssigns["LNKLAST"] = "{$paramsNoImg}&image={$last}";

			$wcThemeAssigns["MAINIMG"] = turlencode("{$wcComic->expandedUri()}{$files[$current]->lpath}");
			$wcThemeAssigns["INFO-IMG-NAME"] = basename($files[$current]->lpath);

			if($index != $first) {
				$wcThemeMobileDefault->addBarIcons(50, "IMAGE_FIRST", "document.location.href='{$wcThemeAssigns["LNKFIRST"]}'", "{$this->uri()}/icons/move-first-icon.png");
				$wcThemeMobileDefault->addBarIcons(51, "IMAGE_PREVIOUS", "document.location.href='{$wcThemeAssigns["LNKPREV"]}'", "{$this->uri()}/icons/move-previous-icon.png");
			}
			$wcThemeMobileDefault->addBarIcons(52, "IMAGE_TABLE", "document.location.href='{$wcThemeAssigns["LNKTABLE"]}'", "{$this->uri()}/icons/table-icon.png");
			if($index != $last) {
				$wcThemeMobileDefault->addBarIcons(53, "IMAGE_NEXT", "document.location.href='{$wcThemeAssigns["LNKNEXT"]}'", "{$this->uri()}/icons/move-next-icon.png");
				$wcThemeMobileDefault->addBarIcons(54, "IMAGE_LAST", "document.location.href='{$wcThemeAssigns["LNKLAST"]}'", "{$this->uri()}/icons/move-last-icon.png");
			}

			$wcThemeAssigns["PAGER-SHOW"] = true;
			$wcThemeAssigns["PAGER-CURRENT"] = $current + 1;
			$wcThemeAssigns["PAGER-COUNT"] = $last + 1;
			$wcThemeAssigns["PAGER-OPENLINK"] = "{$paramsNoImg}&image=";
			$wcThemeAssigns["PAGER-OFFSET"] = 1;
		}

		$wcThemeAssigns["LNKINFO"] = requestParams(true, WC_ACTION_COMIC_INFO, array());
		$wcThemeMobileDefault->addBarIcons(60, "IMAGE_INFO", "document.location.href='{$wcThemeAssigns["LNKINFO"]}'", "{$this->uri()}/icons/edit-info-icon.png");
	} else {
		$wcThemeAssigns["error-string"] = "Invalid comic";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to open comics";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>